package com.sun.hunk.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author sunil
 */
public class DateUtil {

    private String format;
    private Date date;

    public static enum Format {

        MM_DD_YYYY("MM/dd/yyyy"),
        MM_DD_YYYY_HOUR_MINUTE("MM/dd/yyyy kk:mm"),
        DD_MMM_yy("dd MMM yy");
        private String s;

        Format(String s) {
            this.s = s;
        }

        public String toString() {
            return s;
        }
    }

    public static Date parseToDate(String date, Format dateFormat) throws ParseException {
        return parseToDate(date, dateFormat.toString());
    }

    public static Date parseToDate(String date, Format dateFormat, Format nextMathingFormat) throws ParseException {
        try {
            return parseToDate(date, dateFormat.toString());
        } catch (ParseException e) {
            return parseToDate(date, nextMathingFormat.toString());
        }
    }

    public static Date parseToDate(String date, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date parsedDate = null;
        if (null != date && !date.trim().equals("")) {
            parsedDate = (Date) sdf.parse(date.trim());
        }
        return parsedDate;
    }

    public static String autoFormat(Date date) {
        if (! havingTime(date)) {
            return formatToString(date, Format.MM_DD_YYYY);
        } else {
            return formatToString(date, Format.MM_DD_YYYY_HOUR_MINUTE);
        }
    }

    public static String formatToString(Date date, Format dateFormat) {
        return formatToString(date, dateFormat.toString());
    }

    public static String formatToString(Date date, String dateFormat) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formattedDate = null;
        formattedDate = sdf.format(date);
        return formattedDate;
    }

    public static String format(String date, String originalFormat, String convertedFormat) throws ParseException {
        Date _date = parseToDate(date, originalFormat);
        return formatToString(_date, convertedFormat);
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getFomattedDate() {
        if (date == null) {
            return null;
        } else if (format != null) {
            return formatToString(date, format);
        } else {
            return formatToString(date, "dd MMM yyyy ");
        }
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public static boolean havingTime(Date date) {
        if (date != null) {
            return ! (date.getMinutes() == 0 && date.getHours() == 0);
        }
        return false;
    }

    public boolean isHavingTime() {
        if (date != null) {
            return ! (date.getMinutes() == 0 && date.getHours() == 0);
        }
        return false;
    }

    /**
     * Get ( today + addDay ) Date
     * @param addDay - no of days ahead
     * @return - date
     */
    public static Date getDate(int addDay) {
        Calendar tomorrow = new GregorianCalendar();
        tomorrow.add(Calendar.DATE, addDay);
        Calendar result = new GregorianCalendar(
                tomorrow.get(Calendar.YEAR),
                tomorrow.get(Calendar.MONTH),
                tomorrow.get(Calendar.DATE));
        return result.getTime();
    }
}
